package com.nl.idempotent;

import com.nl.idempotent.annotation.IdemCheck;
import com.nl.model.model.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Slf4j
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @IdemCheck(keys = {"#user.name", "#user.age"}, resultType = Boolean.class)
    @Override
    public boolean createUser(User user) {
        log.info("boolean createUser(User user)");
        jdbcTemplate.update("insert into user set name = ?, age = ?", user.getName(), user.getAge());
        return true;
    }

    @IdemCheck(module = "user-model", keys = {"#user.name", "#user.age"}, resultType = User.class)
    @Override
    public User createUser2(User user) {
        log.info("User createUser2(User user)");
        jdbcTemplate.update("insert into user set name = ?, age = ?", user.getName(), user.getAge());
        return new User().setName("测试").setId(new Random().nextLong()).setAge(100);
    }

    @IdemCheck
    @Override
    public void createUser3(User user) {
        log.info("void createUser3(User user)");
    }

    @IdemCheck(module = "user", keys = {"#user.name", "#user.age"}, test = false, resultType = ResponseVO.class)
    @Override
    public ResponseVO<User> createUser4(User user) {
        log.info("User createUser4(User user)");
        return ResponseVO.success(new User().setId(10L).setName("Breggor Hwang").setAge(34));
    }
}
